package 笔试;

import java.util.Scanner;

/**
 * @Date 2024/10/12 10:37
 * @description: 京东笔试: 最长公共前缀
 * .
 * @Author LittleNight
 */
public class JD1 {

    public static void main1(String[] args) {
        String s = "abcd";
        System.out.println(s.substring(0, 2));
    }

    // 测试通过
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            String[] str = new String[n];
            StringBuffer sb = new StringBuffer();
            String ret = "";
            for (int i = 0; i < n; i++) {
                str[i] = in.next();
                if(i == 0) {
                    System.out.println(str[i]);
                    ret = str[0];
                }
                else {
                    ret = langFront(str[i], ret);
                    if(ret.length() == 0) System.out.println("-1");
                    else System.out.println(ret);
                }
            }
        }
    }

    private static String langFront(String s1, String t1) {
        char[] s = s1.toCharArray(), t = t1.toCharArray();
        int m = s.length, n = t.length;
        StringBuffer sb = new StringBuffer();
        int i = 0, j = 0;
        while(i < m && j < n) {
            if(s[i] == t[j]) {
                i++;
                j++;
            } else {
                break;
            }
        }
        // 有一种情况, 出来了
        if(i == m) return s1;
        if(j == n) return t1;
        return s1.substring(0, i + 1); // (]
    }
}
